﻿using BerkeliumDotNet;
using System;
using System.Drawing;
using System.IO;
using System.Threading;

namespace BasicSample
{
    class BasicDemo : WindowDelegate
    {
        int Width = 640;
        int Height = 480;
        string URL = "http://www.google.com";

        Window webWindow;
        bool canPaint = false;
        bool isRunning = true;

        public override void OnPaint(Window window, IntPtr bitmapIn, Rectangle bitmapRect, int dx, int dy, Rectangle scrollRect)
        {
            if (canPaint == false)
                return;
            
            Bitmap bitmap = BerkeliumDotNet.Berkelium.RenderToBitmap(bitmapIn, bitmapRect.Width, bitmapRect.Height);
            bitmap.Save("out.bmp");

            isRunning = false;
        }

        public override void OnLoad(Window window)
        {
            canPaint = true;
        }

        public BasicDemo()
        {
            BerkeliumDotNet.Berkelium.Init();

            webWindow = Window.Create();
            webWindow.SetTransparent(false);
            webWindow.Resize(Width, Height);
            webWindow.SetDelegate(this);
            webWindow.NavigateTo(URL);

            while (isRunning)
            {
                BerkeliumDotNet.Berkelium.Update();
                Thread.Sleep(250);
            }

            webWindow.Dispose();
            BerkeliumDotNet.Berkelium.Destroy();
        }
    }
}
